" Vbundle设置 {{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin('~/.vim/bundle')
" alternatively, pass a path where Vundle should install plugins
" call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'

" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo

Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'
" airline设置
set laststatus=2
" 设置
let g:airline_theme = "luna"

" 使用powerline打过补丁的字体
let g:airline_powerline_fonts = 1
" 开启tabline
let g:airline#extensions#tabline#enabled = 1
" tabline中buffer显示编号
let g:airline#extensions#tabline#buffer_nr_show = 1
" 映射切换buffer的键位
nnoremap b[ :bp<CR>
nnoremap b] :bn<CR>

" git help
" need run PowerlineClearCache refresh and reopen vim to fresh powerline
Plugin 'tpope/vim-fugitive'

" colorscheme
Plugin 'sickill/vim-monokai'
Plugin 'plasticboy/vim-markdown'

" python
" Plugin 'klen/python-mode'
" Override go-to.definition key shortcut to Ctrl-]
" let g:pymode_rope_goto_definition_bind = "<C-]>"
" Override run current python file key shortcut to Ctrl-Shift-e
" let g:pymode_run_bind = "<C-R>"
" Override view python doc key shortcut to Ctrl-Shift-d
" let g:pymode_doc_bind = "<S-K>"

" plugin from http://vim-scripts.org/vim/scripts.html
" Negative
Plugin 'tagbar'
let g:tarbar_ctags_bin = 'ctags'
set tags=tags;
set autochdir
map <F3> :Tagbar<CR>

Plugin 'The-NERD-tree'
map <F2> :NERDTreeToggle<CR>
imap <F2> <ESC>:NERDTreeToggle<CR>
let NERDTreeShowBookmarks=1

" source Explorer
" 和youcompleteme冲突
Plugin 'wesleyche/SrcExpl'
let g:SrcExpl_searchLocalDef = 1
let g:SrcExpl_isUpdateTags = 0
let g:SrcExpl_updateTagsCmd = 'ctags --fields=l --sort=foldcase -R .'
let g:SrcExpl_refreshTime = 1000
nmap <F4> :SrcExplToggle<CR><CR> 
imap <F4> <ESC>:SrcExplToggle<CR><CR>
let g:SrcExpl_pluginList = [ "Source_Explorer" ]

" quick find in screen
Plugin 'easymotion/vim-easymotion'
" define easymotion-prefix 
map <Leader> <Plug>(easymotion-prefix)
" nmap s <Plug>(easymotion-s2)
" nmap t <Plug>(easymotion-t2)
map  <Plug>(easymotion-prefix)/ <Plug>(easymotion-sn)
omap <Plug>(easymotion-prefix)/ <Plug>(easymotion-tn)

" programming with c
Plugin 'firemiles/c.vim'
" 修改 c.vim 的 leader 键，去除和 a.vim 的冲突
let g:C_MapLeader = '\\' 
" swithc between h and cpp
Plugin 'vim-scripts/a.vim'
" auto complete
Plugin 'AutoComplPop'
Plugin 'clang-complete'
if has('mac') || has('macunix')
    let g:clang_library_path="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib"
elseif has('unix')
    let g:clang_library_path=""
endif

let g:clang_use_library=1
let g:clang_auto_select=1 "select first entry in popup menu but don't insert in code
let g:clang_complete_auto=1 "auto complete after -> . and :: 
let g:clang_hl_errors=1         " highlight the warnings and error the same way clang does it
let g:clang_complete_copen=1    " open quickfix window on error
let g:clang_periodic_quickfix=0 " periodically update the quickfix window
let g:clang_snippets=0
let g:clang_close_preview=0
let g:clang_user_options='-stdlib=libc++ -std=c++11'
" 自动关闭补全窗口 
" au CursorMovedI,InsertLeave * if pumvisible() == 0|silent! pclose|endif 
" set completeopt=menuone,menu,longest

" lib
" Plugin 'L9'
" quick find file
Plugin 'ctrlp.vim'
" commenter
Plugin 'scrooloose/nerdcommenter'
" Doxygen comment
Plugin 'DoxygenToolkit.vim'

"Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
" Plugin 'file:///home/gmarik/path/to/plugin'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required

" To ignore plugin indent changes, instead use:
" filetype plugin on
"
" Brief help
" :PluginList       - lists configured plugins
" :PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line
""}}}


" 系统设定   {{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""  
" 解决windows下的中文乱码问题 
"--------------------------------------------------------------------
set fileencodings=utf-8,gbk,chinese  

" 解决中文菜单乱码  
set langmenu=zh_CN.utf-8  
source $VIMRUNTIME/delmenu.vim  
source $VIMRUNTIME/menu.vim  

" 解决console输出乱码  
language messages zh_CN.utf-8

"--------------------------------------------------------------------
" 不要使用vi的键盘模式，而是vim自己的 
set nocompatible 

" history文件中需要记录的行数 
set history=100 

" 在处理未保存或只读文件的时候，弹出确认 
set confirm 

" 载入文件类型插件 
filetype plugin on 

" 为特定文件类型载入相关缩进文件 
filetype indent on 

" 保存全局变量 
set viminfo+=! 

" 带有如下符号的单词不要被换行分割 
set iskeyword+=_,$,@,%,#,- 

" 语法高亮 
syntax on

" 亮字符，让其不受100列限制 
:highlight OverLength ctermbg=red ctermfg=white guibg=red guifg=white 
:match OverLength '\%101v.*' 

" 自动设置目录为正在编辑的文件所在的目录 
set autochdir 
set ffs=unix,dos,mac " favor unix ff, which behaves good under bot Winz & Linux  
set clipboard=unnamed,autoselect,exclude:cons\|linux "set clipboard 

" 用空格键来开关折叠  
set foldenable  
set foldmethod=marker

" 关闭打开文件自动折叠
set foldlevel=99
nnoremap <space> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR>      
" 不要备份文件（根据自己需要取舍） 
set nobackup 

" 不要生成swap文件，当buffer被丢弃的时候隐藏它 
setlocal noswapfile 

" 配色
syntax enable
colorscheme monokai

" 字符间插入的像素行数目  
set linespace=0  

" 显示行号  
set nu  

" 增强模式中的命令行自动完成操作  
set wildmenu  

" 显示命令  
set showcmd  

" 命令行（在状态行下）的高度，默认为1，这里是2  
set cmdheight=1  

" 使回格键（backspace）正常处理indent, eol, start等  
set backspace=2  

" 允许backspace和光标键跨越行边界  
set whichwrap+=<,>,h,l  

" 可以在buffer的任何地方使用鼠标（类似office中在工作区双击鼠标定位）  
set mouse=a  
set selection=exclusive  
set selectmode=mouse,key  

" 启动的时候不显示那个援助索马里儿童的提示  
set shortmess=atI  

" 不让vim发出讨厌的滴滴声  
set noerrorbells  

" 在被分割的窗口间显示空白，便于阅读  
set fillchars=vert:\ ,stl:\ ,stlnc:\  

" 高亮显示匹配的括号  
set showmatch  

" 匹配括号高亮的时间（单位是十分之一秒）  
set matchtime=5  

" 在搜索的时候忽略大小写  
set ignorecase  

" 不要高亮被搜索的句子（phrases）  
set nohlsearch  

" 在搜索时，输入的词句的逐字符高亮（类似firefox的搜索）  
set incsearch  

" 输入:set list命令是应该显示些啥？  
set listchars=tab:\|\ ,trail:.,extends:>,precedes:<,eol:$  

" 光标移动到buffer的顶部和底部时保持3行距离  
set scrolloff=3  

" 不要闪烁  
set novisualbell  

" 自动格式化  
set formatoptions=tcrqn  

" 继承前一行的缩进方式，特别适用于多行注释  
set autoindent  

" 为C程序提供自动缩进  
set smartindent  

" 使用C样式的缩进  
set cindent  

" 空格代替tab
set expandtab

" 制表符为4  
set tabstop=4  

" 统一缩进为4  
set softtabstop=4  
set shiftwidth=4  

" 不要换行  
set wrap  

" 在行和段开始处使用制表符  
set smarttab

" 快捷键设定{{{ 
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""  
" 递归查找cscope.out和tags
" ------------------------------------------------------------------
nmap <F5> :call AutoLoadCTagsAndCScope()<CR>
function! AutoLoadCTagsAndCScope()
    let max = 5
    let dir = './'
    let i = 0
    let break = 0
    let cwd = getcwd()
    while isdirectory(dir) && i < max
        if filereadable(dir . 'cscope.out') 
            execute 'cs add ' . dir . 'cscope.out '. cwd . '/' . dir
            let break = 1
        endif
        if filereadable(dir . 'tags')
            execute 'set tags +=' . dir . 'tags'
            let break = 1
        endif
        if break == 1
            execute 'lcd ' . dir
            break
        endif
        let dir = dir . '../'
        let i = i + 1
    endwhile
endf

nmap <F6> :call MakeAndLoadTags()<CR>
function! MakeAndLoadTags()
    exec '!find ./ -name "*.h" -o -name "*.c" -o -name "*.cpp" -o -name "*.cc"> cscope.files'
    exec '!cscope -bkq -i cscope.files'
    exec '!ctags -R --fields=l ./'
    call AutoLoadCTagsAndCScope()
endfunction

" C的编译和运行 
" ------------------------------------------------------------------
function! CompileRunGcc()  
exec "w"  
exec "!gcc % -o %<"
exec "! ./%<"  
endfunc  

" C++的编译和运行 
" -------------------------------------------------------------------
function! CompileRunGpp()  
exec "w"  
exec "!g++ % -o %<"
exec "! ./%<"  
endfunction

" Python的运行
" --------------------------------------------------------------------
au BufNewFile,BufRead *.py,*.pyw setf python
function! RunPython()   
    let mp = &makeprg   
    let ef = &errorformat   
    let exeFile = expand("%:t")   
    setlocal makeprg=python\ -u   
    set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m   
    silent make %   
    copen   
    let &makeprg     = mp   
    let &errorformat = ef   
endfunction

map <F8> :call Run()<CR>
function! Run()
    if filereadable("Makefile") || filereadable("makefile")
        exec "w"
        exec "make"
        exec "copen"
    elseif &filetype=="c"
        call CompileRunGcc()
    elseif &filetype=="cpp"
        call CompileRunGpp()
    endif
endfunction

" 修改vimrc后自动生效
map <leader>s :source ~/.vimrc<CR>
autocmd! bufwritepost .vimrc source ~/.vimrc 

" ctags设置

" cscope设置
" -------------------------------------------------------------------
if has("cscope")
set csprg=cscope
set csto=0
set cst
set nocsverb
set csverb
endif

" cscope shortcut
nmap <C-@>s :cs find s <C-R>=expand("<cword>")<CR><CR>
nmap <C-@>g :cs find g <C-R>=expand("<cword>")<CR><CR>
nmap <C-@>c :cs find c <C-R>=expand("<cword>")<CR><CR>
nmap <C-@>t :cs find t <C-R>=expand("<cword>")<CR><CR>
nmap <C-@>e :cs find e <C-R>=expand("<cword>")<CR><CR>
nmap <C-@>f :cs find f <C-R>=expand("<file>")<CR><CR>
nmap <C-@>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
nmap <C-@>d :cs find d <C-R>=expand("<cword>")<CR><CR>
""}}}

""}}}


